import { objDeepCopy } from '@/utils'

const decorateField = (field) => {
	const item = field
	if (item.formType == 'category') {
		/** 产品分类 */
		const params = {}
		params['key'] = item.fieldName
		params['data'] = item
		if (
			this.action.type == 'update' ||
			this.action.type == 'copy' ||
			// 2020-7-20 或进行意向转化为客户操作
			(this.action.type == 'transform' && item.value)
		) {
			const value = JSON.parse(item.value)
			params['value'] = value
				? value.map(function (item, index, array) {
						return parseInt(item)
				  })
				: []
		} else {
			params['value'] = []
		}
		params['disabled'] = false // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType === 'table') {
		const params = {}
		params['key'] = item.fieldName
		params['data'] = item
		params['value'] = item.value || []
		params['radio'] = item.radio !== false
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['placeholder'] = item.placeholder
		return params
	} else if (
		item.formType == 'product' ||
		item.formType == 'program' ||
		item.formType == 'product-solution'
	) {
		// 关联产品信息比较多 用字典接收
		const params = {}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['value'] = item.value
		params['createable'] = item.createable !== false
		params['value'] = item.value || []
		params['query'] = item.query || {}
		return params
	} else if (item.formType == 'stageplans') {
		// 关联产品信息比较多 用字典接收
		const params = {}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['value'] = item.value || []
		params['placeholder'] = item.placeholder
		params['allMoney'] = item.allMoney || 0
		return params
	} else if (item.formType == 'datatable' || item.formType == 'datagroup') {
		const params = {}
		params['key'] = item.fieldName
		params['data'] = item
		params['append'] = item.append
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['value'] = item.value || []
		params['placeholder'] = item.placeholder
		params['showblock'] = true
		return params
	} else if (item.formType == 'data-para') {
		const params = {}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['templates'] = item.templates
		params['fields'] = item.fields
		params['placeholder'] = item.placeholder
		params['showblock'] = true
		params['value'] = item.value || Array(item.templates.length)
		return params
	} else if (item.formType == 'wangEditor') {
		const params = {}
		const { files = [], html = '' } = item.value || {}
		params['value'] = { html }
		if (item.isFile == 1) {
			params['value']['files'] = files
		}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'group') {
		const params = {}
		params['value'] = item.value || {}
		params['childrens'] = item.childrens || []
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'unit') {
		const params = {}
		params['units'] = item.units || []
		params['value'] = item.value || {
			unit: params.units.length ? params.units[0].value : null,
			val: ''
		}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'check-text') {
		const params = {}
		params['checkLabel'] = item.checkLabel || ''
		params['value'] = item.value || {
			check: false,
			text: ''
		}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'selectinput') {
		const params = {}
		params['setting'] = item.setting || []
		params['value'] = item.value || {
			type: '',
			value: ''
		}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['placeholder'] = item.placeholder
		params['setting'].forEach((set) => {
			if (set.link?.length) {
				set.link = set.link.map(decorateField)
			}
		})
		return params
	} else if (item.formType == 'mince') {
		const params = {}
		params['value'] = item.value || []
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'map_address') {
		// 关联产品信息比较多 用字典接收
		const params = {}

		if (
			this.action.type == 'update' ||
			this.action.type == 'copy' ||
			// 2020-7-20 或进行意向转化为客户操作
			this.action.type == 'transform'
		) {
			if (item.value.address) {
				item.value.address = item.value.address.split(',')
			}
			params['value'] = item.value // 编辑的值 在value字段
		} else {
			params['value'] = {} // 加入默认值 可能编辑的时候需要调整
		}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = false // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['placeholder'] = item.placeholder
		return params
	} else if (
		item.formType == 'select' ||
		item.formType == 'business_status' ||
		item.formType == 'brand'
	) {
		const params = {}
		params['allowCreate'] = Boolean(item.allowCreate)
		params['filterable'] = Boolean(item.filterable || item.allowCreate) // 下拉框可搜索 (item.allowCreate为真时，item.filterable必须为真)
		// params['value'] = item.value
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['placeholder'] = item.placeholder
		if (item.formType == 'select' || item.formType == 'business_status') {
			params['value'] = item.value ?? ''
		} else {
			params['value'] = item.value || []
		}
		return params
	} else if (item.formType == 'supplier' && item.fieldType == 2) {
		const params = {}
		params['value'] = item.value || []
		params['key'] = item.fieldName
		params['radio'] = item.radio !== false
		params['data'] = item
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'channel') {
		const params = {}
		params['value'] = item.value || []
		params['key'] = item.fieldName
		params['radio'] = item.radio !== false
		params['data'] = item
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'tablex') {
		const params = {}
		params['value'] = item.value || []
		params['key'] = item.fieldName
		params['radio'] = item.radio !== false
		params['data'] = item
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['showblock'] = true // 展示整行效果
		params['placeholder'] = item.placeholder
		params['query'] = item.query || {}
		return params
	} else if (
		// crm相关信息特殊处理
		item.formType == 'customerpersonal' ||
		item.formType == 'businesspersonal' ||
		item.formType == 'relation' ||
		item.formType == 'contractpersonal' ||
		item.formType == 'contacts' ||
		item.formType == 'customer' ||
		item.formType == 'contract' ||
		item.formType == 'business' ||
		item.formType == 'suppliercontacts' ||
		item.formType == 'receivables_plan' ||
		item.formType == 'payable' ||
		item.formType == 'otherPayable' ||
		item.formType == 'supplier' ||
		item.formType == 'person' ||
		item.formType == 'claim'
	) {
		const params = {}
		params['key'] = item.fieldName
		params['data'] = item
		params['radio'] = item.radio !== false
		params['createable'] = item.createable !== false
		// 获取 value relative 信息
		params['value'] = item.value || []
		params['multiple'] = Boolean(item.multiple)
		params['placeholder'] = item.placeholder
		params['query'] = item.query || {}

		params['bigModel'] = item.formType == 'contract'
		return params
	} else if (item.formType == 'multSelect') {
		const params = {}
		params['value'] = item.value || ''
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'date') {
		const params = {}
		params['value'] = item.value || ''
		params['key'] = item.fieldName
		params['data'] = item
		params['type'] = item.type || 'date'
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'dateselect') {
		const params = {}
		params['value'] = item.value || { type: '', dates: [] }
		params['key'] = item.fieldName
		params['data'] = item
		params['counts'] = item.counts
		params['placeholder'] = item.placeholder
		return params
	} else if (item.formType == 'inputrange') {
		const params = {}
		params['value'] = item.value || []
		params['key'] = item.fieldName
		params['data'] = item
		params['placeholders'] = item.placeholders || ['最小值', '最大值']
		return params
	} else if (item.formType == 'list') {
		return item
	} else if (item.formType == 'multTreeSelect') {
		const params = {}
		params['value'] = item.value || []
		params['key'] = item.fieldName
		params['tree'] = item.tree || []
		params['disabled'] = Boolean(item.disabled) // 是否可交互
		params['data'] = item
		params['placeholder'] = item.placeholder
		params['showblock'] = true
		return params
	} else {
		const params = {}
		const value = item.value
		if (
			item.formType == 'user' ||
			item.formType == 'structure' ||
			item.formType == 'file' ||
			item.formType == 'category' ||
			item.formType == 'customer' ||
			item.formType == 'business' ||
			item.formType == 'contract' ||
			item.formType == 'customerpersonal' ||
			item.formType == 'businesspersonal' ||
			item.formType == 'contractpersonal' ||
			item.formType == 'relation'
		) {
			params['value'] = value ? objDeepCopy(value) : []
			params['radio'] = item.radio
		} else {
			params['value'] = value || ''
		}
		params['key'] = item.fieldName
		params['data'] = item
		params['disabled'] = item.disabled // 是否可交互
		params['placeholder'] = item.placeholder
		return params
	}
}

export default decorateField
